package com.intelligent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.pojo.QuotationDetail;
import com.intelligent.service.QuotationDetailService;
import com.intelligent.utils.Result;
import com.intelligent.vo.QuotationDetailPageReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报价明细管理控制器
 * 负责前端接口，业务逻辑由Service层处理
 */
@RestController
@RequestMapping("/quotation-detail")
public class QuotationDetailController {

    @Autowired
    private QuotationDetailService quotationDetailService;

    @PostMapping("/page")
    public Result<Page<QuotationDetail>> pageList(@RequestBody QuotationDetailPageReq quotationDetailPageReq) {
        try {
            Page<QuotationDetail> result = quotationDetailService.pageWithRelations(quotationDetailPageReq);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }


    @PostMapping
    public Result<QuotationDetail> create(@RequestBody QuotationDetail quotationDetail) {
        try {
            QuotationDetail result = quotationDetailService.createQuotationDetail(quotationDetail);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("创建失败：" + e.getMessage());
        }
    }


    @GetMapping("/{id}")
    public Result<QuotationDetail> detail(@PathVariable Long id) {
        try {
            QuotationDetail quotationDetail = quotationDetailService.getQuotationDetailInfo(id);
            if (quotationDetail == null) {
                return Result.error("未找到数据");
            }
            return Result.success(quotationDetail);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }


    @GetMapping("/quotation/{quotationId}")
    public Result<List<QuotationDetail>> getDetailsByQuotationId(@PathVariable Long quotationId) {
        try {
            List<QuotationDetail> details = quotationDetailService.getDetailsByQuotationId(quotationId);
            return Result.success(details);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }


}
